package com.proxy.cjlibProxy;

import com.proxy.service.SmsService;
import com.proxy.service.SmsServiceImpl;
import net.sf.cglib.proxy.Enhancer;


public class CglibProxyFactory {
    public CglibProxyFactory() {
    }

    public static Object getInstance(Object target) {
        Enhancer enhancer = new Enhancer();
        enhancer.setClassLoader(target.getClass().getClassLoader());
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(new CglibMethodInterceptor());
        return enhancer.create();
    }

    public static void main(String[] args) {
        SmsService smsService = (SmsService)getInstance(new SmsServiceImpl());
        smsService.send("短信内容！");
        MySmsService mySmsService = (MySmsService)getInstance(new MySmsService());
        mySmsService.send("未实现接口的类！");
    }
}
